<?php
namespace app\wxapp\controller;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Vod\Vod;
use app\common\entity\Result;
use app\common\service\TopicService;
use app\common\youdao\Assess;
use app\Request;
use app\common\service\VideoService;
use app\wxapp\service\VideoService as WxAppVideoService;
use think\facade\Db;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/10/6 0006
 * Time: 16:59
 */
class Video extends Base{
    private $regionId = 'cn-shanghai';

    public function videoInfo(Request $request){
        $id = $request->param("id");
        $video = Db::table("video_list")->where(['id'=>$id])->find();

        $accessKeyId = env('aliyun.ali_access_key_id');
        $accessKeySecret = env('aliyun.ali_access_key_secret');
        AlibabaCloud::accessKeyClient($accessKeyId, $accessKeySecret)
            ->regionId($this->regionId)
            ->connectTimeout(1)
            ->timeout(3)
            ->asDefaultClient();

        $result = Vod::v20170321()->getPlayInfo()->withVideoId($video['out_video_id'])
            ->format("JSON")
            ->request();

        return Result::success([
                'VideoBase'=>$result['VideoBase'],
                'PlayInfoList'=>$result['PlayInfoList'],
                'subtitle_src'=>"https://medical-course.oss-cn-shenzhen.aliyuncs.com/".$video['subtitle_src'],
                'video'=>$video
            ]
        );

    }

    public function favor(Request $request){
        $id = $request->param('id');
        $service = new VideoService();
        $service->updateFavor($id,$this->user['id']);
        return Result::success("ok");
    }

    /**
     * 获取当前用户是否收藏该视频 和该视频的收藏数量
     * @param Request $request
     * @return mixed
     */
    public function favorStatus(Request $request){
        $videoId = $request->param("id");
        $service = new VideoService();
        $result  = $service->favorStatus($videoId,$this->user['id']);
        return Result::success($result);
    }


    /**
     * 语音测评
     * @param Request $request
     * @return mixed
     */
    public function assess(Request $request){
        $data = $request->post();
        if(empty($data['videoId']) || empty($data['time']) || empty($data['text'])){
            return Result::error('缺少参数');
        }

        $videoId = $data['videoId'];
        $time = $data['time'];
        $text = $data['text'];

        //去掉标点符号
        $text = preg_replace('/[^a-zA-Z0-9\s]+/'," ",$text);

        $service = new WxAppVideoService();
        $data = $service ->assess($videoId,$time,$this->user['id'],$text);
        return Result::success($data);
    }


    /**
     * 获取语音基本信息 其他人语音数量 自己的读音
     * @param Request $request
     * @return \think\Response
     */
    public function voiceBaseInfo(Request $request){
        $data = $request->post();
        if(empty($data['videoId']) || empty($data['time'])){
            return Result::error('缺少参数');
        }

        $videoId = $data['videoId'];
        $time = $data['time'];

        $service = new WxAppVideoService();
        //此方法 现在废了 参数已改
        $result = $service->voiceBaseInfo($videoId,$time,$this->user['id']);

        return Result::success($result);
    }


    /**
     * 获取视频或者专栏
     * @param Request $request
     * @return \think\Response
     */
    public function getList(Request $request){
        //1 视频 2 专栏
        $type = $request->param('type',1);
        $page = $request->param('page',1);
        $pageSize = $request->param('pageSize',20);
        $categoryId = $request->param('categoryId');
        $keyword = $request->param('keyword','');
        $where = [];

        if(!empty($categoryId)){
            array_push($where,[
                'category_id','=',$categoryId
            ]);
        }
        if(!empty($keyword)){
            array_push($where,[
               'title','like','%'.$keyword.'%'
            ]);
        }

        if($type == 1){
            $service = new VideoService();
            $data = $service ->getList($page,$pageSize,$where);
        }else {
            $service = new TopicService();
            $data = $service->getList($page,$pageSize,$where);
        }

        return Result::success($data);
    }


    /**
     * 搜索视频或者专栏  当前只搜视频  因为搜索是混合的  所以办法就是把数据放在一起 用一个表或者 es 来做
     * @param Request $request
     * @return mixed
     */
    public function search(Request $request){
        $keyword = $request->param('keyword','');

    }
}